package com.sysofstudent.web.stu;


import com.sysofstudent.po.Student;
import com.sysofstudent.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.jar.Attributes;

@Controller
@RequestMapping("/student")
public class StudentLoginController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public String loginPage(){return "student/login";}

    @PostMapping("/login")
    public String login(@RequestParam String number,
                        @RequestParam String password,
                        HttpSession session,
                        RedirectAttributes attributes,
                        Model model)
    {
        Student student =studentService.checkStudent(number,password);
        Student student1=studentService.findStudent(number);
        if(student != null) {
            student.setPassword(null);
            session.setAttribute("student", student);
            model.addAttribute("student",student1);
            return "student/personal";
        }else {
            attributes.addFlashAttribute("message","用户名或密码错误");
            return "redirect:/student";
        }
    }
    @GetMapping("/logout")
    public String logout(HttpSession session)
    {
        session.removeAttribute("student");
        return "redirect:/student";
    }


}
